﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.administrator.sources;
using VQData;
using VQWEB.common;


namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for HedaotaoHandler
    /// </summary>
    public class hn_HedaotaoHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region He Dao Tao
            string add_hedaotao = context.Request["add_hedaotao"];
            if (!string.IsNullOrEmpty(add_hedaotao))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_hedaotao>(add_hedaotao);
                context.Response.Write(obj.hedaotao_id);
            }
            string view_hedaotao = context.Request["view_hedaotao"];
            if (!string.IsNullOrEmpty(view_hedaotao))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_hedaotao = context.Request["list_hedaotao"];
            if (!string.IsNullOrEmpty(list_hedaotao))
            {
               RenderList(context);
            }

            string delete_hedaotao = context.Request["delete_hedaotao"];
            if (!string.IsNullOrEmpty(delete_hedaotao))
            {
                 BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                 bussiness.Delete<hn_hedaotao>(delete_hedaotao);
                RenderList(context);
            }

            string lockunlock_hedaotao = context.Request["lockunlock_hedaotao"];
            if (!string.IsNullOrEmpty(lockunlock_hedaotao))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_hedaotao>(lockunlock_hedaotao, "hedaotao_active");
                RenderList(context);
            }
            #endregion
        }

       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void RenderSingle(HttpContext context, int Id)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            object obj = layer.Get<hn_hedaotao>(
               h => h.hedaotao_id.Equals(Id),
               h => new
               {
                   id = h.hedaotao_id,
                   ma = h.hedaotao_id,
                   ten = h.hedaotao_ten,
                   active = h.hedaotao_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
   
        private void RenderList(HttpContext context)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            IList<object> list = layer.List<hn_hedaotao>(
                null,
                null,
                null,
                h=>h.hedaotao_ten,
                 h => new
                 {
                     id = h.hedaotao_id,
                     ma = h.hedaotao_id,
                     ten = h.hedaotao_ten,
                     active = h.hedaotao_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}